// miniprogram/pages/index/index.js
import Notify from '../../miniprogram_npm/@vant/weapp/notify/notify'
import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    newGroupModel: false,
    groupName: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},

  // 展示弹出层
  showNewGroupModel () {
    this.setData({
      newGroupModel: true
    })
  },

  // 确认创建群组
  createGroup (e) {
    let groupName = this.data.groupName
    if (groupName === '') {
      Notify({
        message: '名字不能为空',
        background: '#dc3545',
        selector: '#notify-selector',
        duration: 2000
      })
      // console.log(e.mp.detail.dialog.stopLoading());
      // console.log(this.selectComponent("#new-group-model").stopLoading())
      return
    }
    let self = this
    let dialog = this.selectComponent('#new-group-model')
    console.log(dialog)
    wx.cloud.callFunction({
      name: 'createGroup',
      data: {
        groupName: groupName
      },
      success (res) {
        console.log(Dialog.close())
        Notify({
          message: '新建成功',
          background: '#28a745',
          selector: '#notify-selector',
          duration: 2000
        })
        self.setData({
          newGroupModel: false,
          groupName: ''
        })
        setTimeout(() => {
          wx.switchTab({
            url: '/pages/group/group'
          }),
            2000
        })
      }
    })
    e.detail.dialog.stopLoading()
  },

  // input-change
  onGroupNameChange (e) {
    this.setData({
      groupName: e.detail
    })
  },

  // 关闭弹出层
  closeDialog (e) {
    this.setData({
      newGroupModel: false
    })
    e.detail.dialog.stopLoading()
  }
})
